package xx.pro.shiro.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import xx.pro.shiro.common.Result;
import xx.pro.shiro.enums.ResultCodeEnum;


@Slf4j
@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result ExceptionHandler(AuthorizationException e) {
        // 打印堆栈，以供调试
        e.printStackTrace();
        return Result.fail(ResultCodeEnum.AUTHORITY_UNAUTHORIZED);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result ExceptionHandler(Exception e) {
        // 打印堆栈，以供调试
        e.printStackTrace();
        return Result.fail(ResultCodeEnum.SERVER_ERROR);
    }

}
